package com.shujia.flink.kafka

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}

import java.util.Properties
import scala.io.Source

object Demo2StudnetToKafka {
  def main(args: Array[String]): Unit = {

    /**
     * 1、创建生产者
     *
     */

    val properties = new Properties()

    //指定kafka broker地址
    properties.setProperty("bootstrap.servers", "master:9092,node2:9092,node2:9092")

    //设置key 和value的序列化类
    properties.setProperty("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
    properties.setProperty("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")

    val producer = new KafkaProducer[String, String](properties)


    /**
     * 2、读取学生表将数据批量写入kafka中
     *
     */

    val studentList: List[String] = Source.fromFile("data/students.txt").getLines().toList

    for (student <- studentList) {

      val record = new ProducerRecord[String, String]("student", student)

      //发送数据到kafka中
      producer.send(record)
      producer.flush()

    }

    producer.close()


  }

}
